import { uploadAddress } from "@/config/test"
const respInject = (res:any) => {
    console.log(res)
    if (res.statusCode === 200) {
        if (res.data.code === 0) {
            return res.data.data
        } else {
            uni.showToast({
                icon: 'error',
                title: res.data.errorMsg
            })
            return Promise.reject(res.data)
        }
    } else {
        uni.showToast({
            icon: 'error',
            title: res.errMsg
        })
    }
    return res
}
export const upload = (file:any, formData:any) => {
    return new Promise((resolve, reject) => {
        uni.showLoading()
        uni.uploadFile({
            url: uploadAddress,
            file: file,
            formData: formData,
            filePath: file.path,
            name: 'file',
            success: (res) => {
                console.log(res)
                uni.hideLoading()
                if (res.statusCode === 200) {
                    res.data = JSON.parse(res.data)
                    if (res.data.code === 0) {
                        return resolve(res.data.data)
                    } else {
                        uni.showToast({
                            icon: 'error',
                            title: res.data.errorMsg
                        })
                        return reject(res.data)
                    }
                } else {
                    uni.showToast({
                        icon: 'error',
                        title: '上传失败'
                    })
                }
            },
            fail: (error) => {
                uni.hideLoading()
                reject(error)
            }
        })
    })
    
}